package com.njym.java.security.admin.biz;

import com.github.pagehelper.PageHelper;
import com.njym.java.security.admin.entity.*;
import com.njym.java.security.admin.mapper.FamilyUserGroupsMapper;
import com.njym.java.security.common.biz.BaseBiz;
import com.njym.java.security.common.constant.UserConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import com.njym.java.security.admin.mapper.FamilyUserMapper;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 
 *
 * @author lijie
 * @email 717057076@qq.com
 * @date 2019-11-06 09:41:28
 */
@Service
public class FamilyUserBiz extends BaseBiz<FamilyUserMapper,FamilyUser> {
    @Resource
    private FamilyUserMapper familyUserMapper;
    @Autowired
    private FamilyUserGroupsBiz familyUserGroupsBiz;
    @Autowired
    private UserGroupsToUserBiz userGroupsToUserBiz;
    @Resource
    private FamilyUserGroupsMapper familyUserGroupsMapper;

    @Override
    public void insertSelective(FamilyUser familyUser) {
        String password = new BCryptPasswordEncoder(UserConstant.PW_ENCORDER_SALT).encode(familyUser.getPassword());
        familyUser.setPassword(password);
        familyUserMapper.insertFamilyUser(familyUser);
        int id = familyUser.getId();
        FamilyUserGroups familyUserGroups = new FamilyUserGroups();
        UserGroupsToUser userGroupsToUser = new UserGroupsToUser();
        if(familyUser.getPid() == 0){

            familyUserGroups.setIsFamily(1);
            familyUserGroups.setUgName(familyUser.getNickName());
            familyUserGroups.setUgCreateTime(new Date());
            familyUserGroups.setUsAdminId(id);
            familyUserGroups.setUgNum(1);
            familyUserGroupsBiz.addFamilyGroup(familyUserGroups);
            int groupId = familyUserGroups.getId();

            userGroupsToUser.setUserId(id);
            userGroupsToUser.setGroupId(groupId);
            userGroupsToUser.setCreateTime(new Date());
            userGroupsToUser.setGroupNick(familyUser.getNickName());
            userGroupsToUserBiz.insertSelective(userGroupsToUser);
        }else{
            familyUserGroupsMapper.updateNum(familyUser.getPid());
            Example example = new Example(FamilyUserGroups.class);

            example.createCriteria().andEqualTo("usAdminId", familyUser.getPid()).andEqualTo("isFamily", 1);
            List<FamilyUserGroups> groups = familyUserGroupsBiz.selectByExample(example);
            userGroupsToUser.setUserId(id);
            userGroupsToUser.setGroupId(groups.get(0).getId());
            userGroupsToUser.setCreateTime(new Date());
            userGroupsToUser.setGroupNick(familyUser.getNickName());

            userGroupsToUserBiz.insertSelective(userGroupsToUser);
        }
        if(familyUser.getPid() == 0) {

            familyUser.setPid(id);
            super.updateSelectiveById(familyUser);
        }
    }

    public List<FamilyUser> queryFamilyZhu(){
        return familyUserMapper.queryFamilyZhu();
    }

    public List<FamilyUser> queryIndirect(String start, String end, Integer page, Integer limit){
        PageHelper.startPage(page, limit);
        return familyUserMapper.queryIndirect(start, end);
    }

    public List<FamilyUserVo> getListAndPercent(Integer teacherId, String name, String mobile) {
        return familyUserMapper.getListAndPercent(teacherId, name, mobile);
    }

    public int selectCounts(Integer teacherId, String name, String mobile) {
        return familyUserMapper.selectCounts(teacherId, name, mobile);
    }

    public List<FamilyUser> query365(String start, String end){

        return familyUserMapper.query365(start, end);
    }

    public List<FamilyUserVo> queryDaiLiRegister(String start, String end, Integer page, Integer limit){
        PageHelper.startPage(page, limit);
        return familyUserMapper.queryDaiLiRegister(start, end);
    }

    public List<FamilyUserVo> queryDaiLiRegisterAll(String start, String end){

        return familyUserMapper.queryDaiLiRegister(start, end);
    }

    public List<FamilyUser> queryNoRegister(String start, String end, Integer page, Integer limit){
        PageHelper.startPage(page, limit);
        return familyUserMapper.queryNoRegister(start, end);
    }

    public List<FamilyUser> queryNoRegisterAll(String start, String end){

        return familyUserMapper.queryNoRegister(start, end);
    }

    public Integer getDailiCounts() {
        return mapper.getDailiCounts();
    }

    public List<FamilyUser> getList(Integer page, Integer limit,String name, String mobile,String createdTime){
        PageHelper.startPage(page, limit);
        return mapper.getList(name, mobile, createdTime);
    }

    /**
     * 普通用户推广的数据
     * @param page
     * @param limit
     * @param name
     * @param mobile
     * @return
     */
    public List<FamilyUserVo> getZhiJieCount(Integer page, Integer limit,String name, String mobile){
        PageHelper.startPage(page, limit);
        return mapper.getZhiJieCount(name, mobile);
    }
}